import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ConfigService } from '@nestjs/config';
import * as net from 'net';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const configService = app.get(ConfigService);
  const port = configService.get<number>('server.port');

  // Create TCP server
  const server = net.createServer((socket) => {
    console.log('Client connected');

    socket.on('data', (data) => {
      console.log('Received data:', data.toString());
      // Echo back the received data
      socket.write(data);
    });

    socket.on('end', () => {
      console.log('Client disconnected');
    });

    socket.on('error', (err) => {
      console.error('Socket error:', err);
    });
  });

  server.listen(port, () => {
    console.log(`TCP Server is running on port ${port}`);
  });
}
bootstrap();
